from pynixweb.db.database import ConnectionFactory
import os

class TransactionsMiddleware(object):
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        connection = ConnectionFactory.getConnection()
        result = None
        try:
            result = self.app(environ, start_response)
        except Exception, e:
            raise e
            connection.rollback()
        finally:
            connection.close()
        return result
        